package com.hl.component;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.heliang.common.FileLog;

/**
 * 
 * @ClassName: LoadindImageView.java
 * @Description: 异步加载图片时辅助显示类
 * 
 * @author heliang
 * @version V1.0
 * @Date 2013-10-15 下午4:45:57
 */
public class LoadindImageView extends LoadingView
{
    private ImageView iv;

    private boolean isBg;// 设置图片是否设置成imageview背景

    public LoadindImageView(Context context, Bitmap bitmap, boolean isBg)
    {
        super(context);
        this.mContext = context;
        // this.bitmap = bitmap;
        this.isBg = isBg;
        iv = new ImageView(context);
        initUI(bitmap);
    }

    public LoadindImageView(Context context, Drawable drawable, boolean isBg)
    {
        super(context);
        this.mContext = context;
        // this.bitmap = bitmap;
        this.isBg = isBg;
        iv = new ImageView(context);
        initUI(drawable);
    }

    public LoadindImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.mContext = context;
        iv = new ImageView(context);

    }

    public void setImageViewSource(Bitmap bitmap)
    {
        // this.bitmap = bitmap;
        initUI(bitmap);
    }

    /**
     * 微信功能
     * @return
     */
    public ImageView getIV()
    {
        return this.iv;
    }

    @Override
    protected void initUI(Bitmap bitmap)
    {
        try
        {
            if (bitmap == null)
            {
                return;
            }

            FrameLayout fl = new AppFrameLayout(this.mContext);
            FrameLayout.LayoutParams lpf = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            lpf.gravity = Gravity.CENTER;
            fl.setLayoutParams(lpf);

            ProgressBar pb = new ProgressBar(this.mContext);

            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lp.gravity = Gravity.CENTER;
            pb.setLayoutParams(lp);
            if (isBg)
            {
                iv.setBackgroundDrawable(new BitmapDrawable(bitmap));
            }
            else
            {
                iv.setImageBitmap(bitmap);
            }

            iv.setAdjustViewBounds(true);

            this.addView(iv, 0);
            fl.addView(pb);
            this.addView(fl, 1);
        }
        catch (Exception e)
        {
            // TODO: handle exception
            // Log.e(Constants.TAG, e.getMessage());
            FileLog.log("CategoryView.initUI " + e.getMessage());
        }

    }

    @Override
    protected void initUI(Drawable drawable)
    {
        try
        {

            if (drawable == null)
            {
                return;
            }

            FrameLayout fl = new AppFrameLayout(this.mContext);
            FrameLayout.LayoutParams lpf = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            lpf.gravity = Gravity.CENTER;
            fl.setLayoutParams(lpf);

            ProgressBar pb = new ProgressBar(this.mContext);

            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lp.gravity = Gravity.CENTER;
            pb.setLayoutParams(lp);
            if (isBg)
            {
                iv.setBackgroundDrawable(drawable);
            }
            else
            {
                iv.setImageDrawable(drawable);
            }

            iv.setAdjustViewBounds(true);

            this.addView(iv, 0);
            fl.addView(pb);
            this.addView(fl, 1);
        }
        catch (Exception e)
        {
            // TODO: handle exception
            // Log.e(Constants.TAG, e.getMessage());
            FileLog.log("CategoryView.initUI " + e.getMessage());
        }

    }
}
